package com.changhai.diabetes.service;

import java.util.HashMap;
import java.util.Map;

import com.arcsoft.frameworkcommons.dao.BaseDao;
import com.changhai.diabetes.dao.BaseDaoImpl;
import com.changhai.diabetes.util.MySqlMapClientFactoryBean;

public class BaseService {
	private BaseDao dao;
	private boolean initialized = false;
	private static Map<String, BaseDao> daoMap = new HashMap<String, BaseDao>();

	public BaseDao getDao() {
		if (!initialized) {
			initDao();
		}
		return getDao(null);
	}

	public void setDao(BaseDao dao) {
		this.dao = dao;
	}

	public BaseDao getDao(String dsName) {
		if (!initialized) {
			initDao();
		}
		if (dsName == null) {
			return getDao(MySqlMapClientFactoryBean.getDefaultDataSourceName());
		}
		BaseDao baseDao = (BaseDaoImpl) daoMap.get(dsName);
		return baseDao;
	}

	public synchronized void initDao() {
		if (!initialized) {
			for (Object o : BaseDaoImpl.getTemplateMap().keySet()) {
				BaseDaoImpl baseDao = new BaseDaoImpl();
				baseDao.setSqlMapClientTemplate(BaseDaoImpl.getTemplateMap().get(o));
				baseDao.afterPropertiesSet();
				daoMap.put(o.toString(), baseDao);
			}
			initialized = true;
		}
	}
}
